package com.markerhub.controller;


import com.markerhub.common.ResponseJson;
import com.markerhub.service.TagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 关注公众号：MarkerHub
 * @since 2021-04-10
 */
@Api(value = "TypeController",tags = "博客标签接口")
@RestController
@RequestMapping("/tag")
public class TagController {

    @Autowired
    TagService tagService;

    @ApiOperation(value = "分页查询标签数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页号",required = true,dataType = "int"),
            @ApiImplicitParam(name = "size",value = "页面大小",required = true,dataType = "int"),
            @ApiImplicitParam(name = "column",value = "排序字段",required = true,dataType = "string"),
            @ApiImplicitParam(name = "sort",value = "升序0降序1",required = true,dataType = "int"),
            @ApiImplicitParam(name = "querycontent",value = "搜索字段",required = false,dataType = "string"),
    })
    @GetMapping("/taglist")
    public Object getTagList(Integer current,Integer size,String column,Integer sort,String querycontent){
        try{
            return ResponseJson.success(tagService.getTags(current,size,column,sort,querycontent));
        }catch (Exception e)
        {
            return ResponseJson.failure(e.getMessage());
        }
    }

    @ApiOperation(value = "添加标签")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tagname",value = "标签名称",required = true,dataType = "string"),
    })
    @PostMapping("/addtag")
    public Object addTag(String tagname){
        try{
            tagService.addtag(tagname);
            return ResponseJson.success();
        }catch (Exception e)
        {
            return ResponseJson.failure(e.getMessage());
        }
    }

    @ApiOperation(value = "删除标签")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tagid",value = "标签id",required = true,dataType = "int"),
    })
    @DeleteMapping("/deletetag")
    public Object deleteTag(Integer tagid){
        try{
            tagService.deletetag(tagid);
            return ResponseJson.success();
        }catch (Exception e)
        {
            return ResponseJson.failure(e.getMessage());
        }
    }

    @ApiOperation(value = "修改标签")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tagid",value = "标签id",required = true,dataType = "int"),
            @ApiImplicitParam(name = "tagname",value = "标签名称",required = true,dataType = "string"),
    })
    @PostMapping("/edit_tag")
    public Object editTag(Integer tagid,String tagname){
        try{
            tagService.edit_tag(tagid,tagname);
            return ResponseJson.success();
        }catch (Exception e)
        {
            return ResponseJson.failure(e.getMessage());
        }
    }
}
